﻿function RomanNumerals() {
  this.integerToRoman = function (number) {
    if (isNaN(number))
      return 'invalid input';

    if (number < 1 || number > 3000)
      return 'the number must be between 1 and 3000';

    var sequence = (number + '').split('');
    var roman = '';
    for (var i = 0; i < sequence.length; i++) {
      roman = roman + getPart(parseInt(sequence[i]), (sequence.length - 1) - i);
    }
    return roman;
  };

  function getPart(number, index) {
    var vocabulary = new Array();
    vocabulary.push(new Array('I', 'V', 'X'));
    vocabulary.push(new Array('X', 'L', 'C'));
    vocabulary.push(new Array('C', 'D', 'M'));
    vocabulary.push(new Array('M', '', ''));

    var roman = '';
    if (number == 9) {
      roman = vocabulary[index][2];
      number = number - 10;
    }
    if (number >= 4) {
      roman = vocabulary[index][1];
      number = number - 5;
    }
    for (var i = 0; i < number; i++) {
      roman = roman + vocabulary[index][0];
    }
    for (var j = number; j < 0; j++) {
      roman = vocabulary[index][0] + roman;
    }
    return roman;
  }
}
